package com.taoyibao.hongcanting.ui.restaurant.delegate;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.kymjs.frame.view.AppDelegate;
import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.ui.adapter.MyTabFragmentAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author TangAnna
 * @description:
 * @date :${DATA} 15:24
 */
public class GiftFragmentDelegate extends AppDelegate implements ViewPager.OnPageChangeListener {
    private ViewPager mViewPager;
    private List<Fragment> mFragments;
    private MyTabFragmentAdapter mAdapter;
    private LinearLayout mLinearLayout;
    private List<ImageView> mImageViews;


    @Override
    public int getRootLayoutId() {
        return R.layout.fragment_gift;
    }

    @Override
    public void initWidget() {
        super.initWidget();
        mLinearLayout = get(R.id.layout_indicator);
        mViewPager = get(R.id.vp_gift);
        mViewPager.addOnPageChangeListener(this);
        mFragments = new ArrayList<>();
        mImageViews = new ArrayList<>();
    }

    public void setData(FragmentManager mFragmentManager, List<Fragment> fragmentList) {
        if (fragmentList == null) {
            return;
        }
        mFragments.addAll(fragmentList);
        mAdapter = new MyTabFragmentAdapter(mFragmentManager, mFragments);
        mViewPager.setAdapter(mAdapter);
        mViewPager.setOffscreenPageLimit(mFragments.size());
        createIndicator(mFragments.size());
    }

    private void createIndicator(int count) {
        for (int i = 0; i < count; i++) {
            ImageView imageView = new ImageView(getActivity());
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(20, 20);
            params.leftMargin = 4;
            params.rightMargin = 4;
            imageView.setImageResource(R.drawable.selector_dot_f46_666);

            if (i == 0) {
                imageView.setEnabled(false);
            } else {
                imageView.setEnabled(true);
            }
            mImageViews.add(imageView);
            mLinearLayout.addView(imageView, params);
        }
    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        for (int j = 0; j < mImageViews.size(); j++) {
            if (i == j) {
                mImageViews.get(j).setEnabled(false);
            } else {
                mImageViews.get(j).setEnabled(true);
            }
        }
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

}
